<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript">
			function jieCheng() {
				var num = parseInt(document.getElementById('num').value);
				var span = document.getElementById('result');
				if (isNaN(num)) {
					span.innerHTML = '请输入一个数字';
					return;
				}
				
				if (num < 0) {
					span.innerHTML = '负数没有阶乘';
				} else if (num == 0) {
					span.innerHTML = '1';
				} else {
					// n! = n*(n-1)*(n-2)...3*2*1
					var result = 1;
					for (var i = num; i >= 1; i--) {
						console.log(i);
						result = result * i;
					}
					span.innerHTML = result;
				}
			}
		</script>
	</head>
	<body>
		<input type="text" id="num" /> 
		<input type="button" onclick="jieCheng()"  value="计算阶乘"/>
		<span id="result"></span>
		
	</body>
</html>
